package com.revenant.driver.ui.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.App;
import com.revenant.driver.R;
import com.revenant.driver.base.BaseActivity;
import com.revenant.driver.bean.BaseBean.MessageEvent;
import com.revenant.driver.bean.BigItemBean;
import com.revenant.driver.bean.Driver.CarHeadNumSelectBean;
import com.revenant.driver.bean.ShipperBean.PersonalDetailBean;
import com.revenant.driver.ui.activity.newversion.Test;
import com.revenant.driver.ui.view.DiverMineBigItem;
import com.revenant.driver.utils.Constants;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.revenant.driver.bean.BaseBean.MyEventCode.RenZhengCode;
import static com.revenant.driver.bean.Message.Driver_RenZheng_6;
import static com.revenant.driver.bean.Message.Driver_RenZheng_7;
import static com.revenant.driver.bean.Message.Driver_RenZheng_8;
import static com.revenant.driver.utils.Constants.Get_personal_info;
import static com.revenant.driver.utils.Constants.Improve_Car_Head_Num_Beans;
import static com.revenant.driver.utils.Constants.Warning_Mine_HeadText;
import static com.revenant.driver.utils.Constants.Warning_Text;
import static com.revenant.driver.utils.Constants.updateAuroraEquipment;

public class DriverMineActivity extends BaseActivity {
    @BindView(R.id.mine_personal_image)
    ImageView minePersonalImage;
    @BindView(R.id.phone_num)
    TextView phoneNum;
    @BindView(R.id.personal_status)
    TextView personalStatus;
    @BindView(R.id.edit_personal_info)
    TextView editPersonalInfo;
    @BindView(R.id.big_back)
    ImageView bigBack;
    @BindView(R.id.improve_driver_info)
    TextView improveDriverInfo;
    @BindView(R.id.improve_driver_info_btn)
    RelativeLayout improveDriverInfoBtn;
    @BindView(R.id.mine_bigitem_titleone)
    TextView mineBigitemTitleone;
    @BindView(R.id.mine_bigitem_titletwo)
    TextView mineBigitemTitletwo;
    @BindView(R.id.item_mine_right_click)
    LinearLayout itemMineRightClick;
    @BindView(R.id.mybacket_text)
    TextView mybacketText;
    @BindView(R.id.show_money_click)
    TextView showMoneyClick;
    @BindView(R.id.TransportOrder)
    DiverMineBigItem transportOrder;
    @BindView(R.id.ShipperAssistant)
    DiverMineBigItem ShipperAssistant;
    @BindView(R.id.ShipperService)
    DiverMineBigItem ShipperService;
    @BindView(R.id.tv_device_name)
    TextView tvDeviceName;
    private boolean isshowwallet = true;

    private List<BigItemBean> transportOrderList;
    private List<String> transportOrderTitles = Arrays.asList("我的运单",
            "待装货",
            "进行中",
            "待结算",
//            "待确认",
//            "待评价",
            "已完成",
            "卸货"
    );
    private List<Drawable> transportOrderDrawable;
    private List<Class> transportOrderClassList;


    private List<BigItemBean> ShipperAssistantList;
    private List<String> ShipperAssistantTitles = Arrays.asList("发布空车", "车辆管理", "评价管理", "银行卡管理");
    //    private List<String> ShipperAssistantTitles = Arrays.asList("发布货源", "我的发票", "历史发票", "我的模板", "我的熟车", "货主圈", "公司成员");
    private List<Drawable> ShipperAssistantDrawable;
    private List<Class> ShipperAssistantClassList;


    private List<BigItemBean> ShipperServiceList;
    private List<String> ShipperServiceTitles = Arrays.asList("客户服务", "鲁通卡ETC", "优惠加油", "保险", "App分享", "设置");
    private List<Drawable> ShipperServiceDrawable;
    private List<Class> ShipperServiceClassList;

    private String showwallet = "";


    private Context context;

    @Override
    public int getContentViewResId() {
        context = this;
        return R.layout.activity_driver_mine;
    }

    @Override
    public void initView() {
//        if (SPUtils.getUserAuthentication(this).equals("0")) {
////            improveDriverInfoBtn.setVisibility(View.VISIBLE);
////            getpseronal();
////
////        } else {
////            improveDriverInfoBtn.setVisibility(View.GONE);
        getpseronal();

//        }
        showrightimgae(true);

        setLeftBarHide(false);

        select_head_num();
    }

    @Override
    protected boolean isshowtitlebar() {
        return true;
    }

    @Override
    public void initData() {
        transportOrder.titleone("我的订单");
        transportOrder.titletwo("其他订单 >");

        transportOrderDrawable = new ArrayList<Drawable>();
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order0));
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order1));
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order2));
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order3));
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order4));
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order5));
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order6));
        transportOrderDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.order7));

        transportOrderList = new ArrayList<>();

        for (int i = 0; i < transportOrderTitles.size(); i++) {
            BigItemBean bigItemBean = new BigItemBean();
            bigItemBean.setTitle(transportOrderTitles.get(i));
            bigItemBean.setDrawable(transportOrderDrawable.get(i));
            transportOrderList.add(bigItemBean);
        }

        transportOrderClassList = new ArrayList<>();
        transportOrderClassList.add(DriverOrderActivity.class);
        transportOrderClassList.add(DriverOrderActivity.class);
        transportOrderClassList.add(DriverOrderActivity.class);
        transportOrderClassList.add(DriverOrderActivity.class);
//        transportOrderClassList.add(DriverOrderActivity.class);
        transportOrderClassList.add(DriverOrderActivity.class);
        transportOrderClassList.add(DriverOrderActivity.class);
        transportOrderClassList.add(DriverOrderActivity.class);
        transportOrder.setActivity(transportOrderClassList);
        transportOrder.setData(transportOrderList);


        ShipperAssistant.titleone("车主助手");
        ShipperAssistant.titletwo("全部功能 >");

        ShipperAssistantDrawable = new ArrayList<Drawable>();
        ShipperAssistantDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.supply));
        ShipperAssistantDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.acquaintance));
        ShipperAssistantDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.members));
        ShipperAssistantDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.card));

        ShipperAssistantList = new ArrayList<>();

        for (int i = 0; i < ShipperAssistantTitles.size(); i++) {
            BigItemBean bigItemBean = new BigItemBean();
            bigItemBean.setTitle(ShipperAssistantTitles.get(i));
            bigItemBean.setDrawable(ShipperAssistantDrawable.get(i));
            ShipperAssistantList.add(bigItemBean);
        }

        ShipperAssistantClassList = new ArrayList<>();
        ShipperAssistantClassList.add(DriverEmptyCarManageActivity.class);
        ShipperAssistantClassList.add(DriverVehicleManageActivity.class);
        ShipperAssistantClassList.add(CommentActivity.class);
        ShipperAssistantClassList.add(WalletWebActivity.class);


        ShipperAssistant.setActivity(ShipperAssistantClassList);
        ShipperAssistant.setData(ShipperAssistantList);


        ShipperService.titleone("车主服务");
        ShipperService.titletwo("全部功能 >");

        ShipperServiceDrawable = new ArrayList<Drawable>();
        ShipperServiceDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.client));
        ShipperServiceDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.dispose));
        ShipperServiceDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.card));
        ShipperServiceDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.card));
        ShipperServiceDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.share));
        ShipperServiceDrawable.add(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.setting));
        ShipperServiceList = new ArrayList<>();

        for (int i = 0; i < ShipperServiceTitles.size(); i++) {
            BigItemBean bigItemBean = new BigItemBean();
            bigItemBean.setTitle(ShipperServiceTitles.get(i));
            bigItemBean.setDrawable(ShipperServiceDrawable.get(i));
            ShipperServiceList.add(bigItemBean);
        }

        ShipperServiceClassList = new ArrayList<>();
        ShipperServiceClassList.add(AdvicesActivity.class);
        ShipperServiceClassList.add(null); /*鲁通卡ETC*/
        ShipperServiceClassList.add(OilWebActivity.class); /*优惠加油*/


        if (Constants.isProOrTest) {
            ShipperServiceClassList.add(null); /*保险*/
        } else {
            ShipperServiceClassList.add(Test.class); /*优惠加油*/
        }
        ShipperServiceClassList.add(null); /*APP分享*/
        ShipperServiceClassList.add(SettingActivity.class);
        ShipperService.setActivity(ShipperServiceClassList);
        ShipperService.setData(ShipperServiceList);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({
            R.id.big_back,
            R.id.edit_personal_info,
            R.id.improve_driver_info_btn,
            R.id.item_mine_right_click,
            R.id.mine_personal_image,
            R.id.show_money_click,
            R.id.tv_device_name
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_device_name:
                Bundle bundlecar = new Bundle();
                bundlecar.putInt("cartype", 1);
                startActivity(BuQuanGuaCheActivity.class, bundlecar);
                break;

            case R.id.big_back:
                finish();
                break;
            case R.id.mine_personal_image:
            case R.id.edit_personal_info:
                App applicationContext = (App) getApplicationContext();
                int userStatus = applicationContext.getUserStatus();
                switch (userStatus) {
                    case 5:
                        startActivity(PersonalActivity.class);
                        break;
                    case 6:
                        showToast(Driver_RenZheng_6);
                        startActivity(NewImproveDriverInfoaActivity.class);
//                        startActivity(DriverRenZhengOneActivity.class);
                        break;
                    case 7:
                        showToast(Driver_RenZheng_7);
                        break;
                    case 8:
                        showToast(Driver_RenZheng_8);
                        startActivity(NewImproveDriverInfoaActivity.class);
//                        startActivity(DriverRenZhengOneActivity.class);
                        break;
                    default:
                }
                break;
            case R.id.improve_driver_info_btn:
                App applicationContextrz = (App) getApplicationContext();
                int userStatusrz = applicationContextrz.getUserStatus();
                switch (userStatusrz) {

                    case 6:
                        showToast(Driver_RenZheng_6);
//                        startActivity(DriverRenZhengOneActivity.class);
                        startActivity(NewImproveDriverInfoaActivity.class);
                        break;
                    case 7:
                        showToast(Driver_RenZheng_7);
                        break;
                    case 8:
                        showToast(Driver_RenZheng_8);
//                        startActivity(DriverRenZhengOneActivity.class);
                        startActivity(NewImproveDriverInfoaActivity.class);
                        break;
                    default:
                }
                break;
            case R.id.item_mine_right_click:

                App applicationContextqb = (App) getApplicationContext();
                int userStatusqb = applicationContextqb.getUserStatus();
                switch (userStatusqb) {
                    case 5:
                        startActivity(WalletWebActivity.class);
                        break;
                    case 6:
                        showToast(Driver_RenZheng_6);
//                        startActivity(DriverRenZhengOneActivity.class);
                        startActivity(NewImproveDriverInfoaActivity.class);
                        break;
                    case 7:
                        showToast(Driver_RenZheng_7);
                        break;
                    case 8:
                        showToast(Driver_RenZheng_8);
//                        startActivity(DriverRenZhengOneActivity.class);
                        startActivity(NewImproveDriverInfoaActivity.class);
                        break;
                    default:
                }
                break;
            case R.id.show_money_click:

                App applicationContextrzmoney = (App) getApplicationContext();
                int userStatusrz_showMoneyClick = applicationContextrzmoney.getUserStatus();
                switch (userStatusrz_showMoneyClick) {
                    case 5:
                        isshowwallet = SPUtils.getUserShowwWallet(this);
                        isshowwallet(!isshowwallet);
                        SPUtils.setUserShowwWallet(this, !isshowwallet);
                        break;
                    case 6:
                        showToast(Driver_RenZheng_6);
//                        startActivity(DriverRenZhengOneActivity.class);
                        startActivity(NewImproveDriverInfoaActivity.class);
                        break;
                    case 7:
                        showToast(Driver_RenZheng_7);
                        break;
                    case 8:
                        showToast(Driver_RenZheng_8);
//                        startActivity(DriverRenZhengOneActivity.class);
                        startActivity(NewImproveDriverInfoaActivity.class);
                        break;
                    default:
                }
                break;
        }
    }


    private void getpseronal() {
        showLoadingView();
        OkGo.<String>get(Get_personal_info)

                .params("id", SPUtils.getAccounId(context))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        PersonalDetailBean g = GsonUtil.parseJsonWithGson(response.body(), PersonalDetailBean.class);
                        if (g.getCode().equals("0")) {
                            if (g.getData().getAuroraEquipment()!=null){
                                if (g.getData().getAuroraEquipment().isEmpty()){
                                    if (!SPUtils.getJpRidToken(context).isEmpty()) {
                                        updateAuroraEquipment(SPUtils.getJpRidToken(context));
                                    }
                                }
                            }


                            if (g.getData().getMobile() != null) {
                                SPUtils.setUsermobile(context, g.getData().getMobile());
                                StringBuilder stringBuilder = new StringBuilder(g.getData().getMobile());
                                if (stringBuilder.length() > 0) {
                                    stringBuilder.replace(3, 7, "****");
                                    if (stringBuilder != null) {
                                        if (!stringBuilder.toString().isEmpty()) {
                                            phoneNum.setText(stringBuilder.toString());
                                        }
                                    }


                                }
                            } else {
                                phoneNum.setText("***");
                            }

                            Glide.with(context).load(g.getData().getPhoto()).placeholder(R.mipmap.picture).centerCrop().
                                    into(minePersonalImage);
                            showwallet = g.getData().getBalance();

                            App applicationContext = (App) getApplicationContext();
                            applicationContext.setUserStatus(Integer.valueOf(g.getData().getStatus()));
                            int userStatus = applicationContext.getUserStatus();
                            isshowwallet = SPUtils.getUserShowwWallet(context);
                            isshowwallet(SPUtils.getUserShowwWallet(context));
                            switch (userStatus) {
                                case 5:
                                    personalStatus.setText("已认证");
                                    improveDriverInfoBtn.setVisibility(View.GONE);
                                    break;
                                case 6:
                                    personalStatus.setText("未通过");
                                    improveDriverInfoBtn.setVisibility(View.VISIBLE);
                                    improveDriverInfo.setText("未通过 >");
                                    break;
                                case 7:
                                    personalStatus.setText("待审核");
                                    improveDriverInfoBtn.setVisibility(View.VISIBLE);
                                    improveDriverInfo.setClickable(false);
                                    improveDriverInfo.setText("待审核 >");
                                    break;
                                case 8:
                                    personalStatus.setText("未认证");
                                    improveDriverInfoBtn.setVisibility(View.VISIBLE);
                                    improveDriverInfo.setText("未认证 >");
                                    break;
                                default:
                            }

                        }
                        showContentView();

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showErrorView();
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if (!SPUtils.getUserAuthentication(this).equals("0")) {
        getpseronal();
        select_head_num();
//        }
    }

    @Override
    public void rightbasebarimageclick() {
        super.rightbasebarimageclick();
        startActivity(NewsActivity.class);
    }

    public void isshowwallet(boolean isshowwallet) {
        if (isshowwallet) {
            mybacketText.setText(showwallet);
        } else {
            mybacketText.setText("*****");

        }
    }

    /*无网络*/
    @Override
    public void doNoNetSomething() {
        showErrorView();
        LogUtils.d("whatnetnonet");
    }


    /*有网络*/
    @Override
    public void doNoCheckNetSomething() {
        LogUtils.d("whatnetnet");
        getpseronal();
    }

    /*重新网络*/
    @Override
    public void onNetworkViewRefresh() {
        LogUtils.d("whatnetrefreshnet");
        getpseronal();
    }

    @Override
    protected void receiveStickyEvent(MessageEvent event) {
        super.receiveStickyEvent(event);
        switch (event.getCode()) {
            case RenZhengCode:
                getpseronal();
                break;
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }


    private void select_head_num() {
        OkGo.<String>get(Improve_Car_Head_Num_Beans)
                .params("driverId", SPUtils.getAccounId(context))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200) {
                            CarHeadNumSelectBean personal = GsonUtil.parseJsonWithGson(response.body(), CarHeadNumSelectBean.class);
                            if (Integer.valueOf(personal.getCode()) == 0) {

                                if (personal.getData().getIsConsummate() == 0) {
                                    tvDeviceName.setVisibility(View.VISIBLE);
                                    tvDeviceName.setText(Warning_Mine_HeadText);
                                    tvDeviceName.setSelected(true);
                                }else {
                                    tvDeviceName.setVisibility(View.GONE);
                                }

                            } else {

                            }

                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast(response.message());
                    }
                });
    }

    private void updateAuroraEquipment(String auroraEquipment) {
        OkGo.<String>post(updateAuroraEquipment)
                .params("id", SPUtils.getAccounId(context))
                .params("auroraEquipment", auroraEquipment)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {


                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast(response.message());
                    }
                });
    }
}
